import { config } from '../config.js'


  var request = function({url, data={}, method="GET"}) {
    return new Promise((resolve, reject)=>{
      goRequest(url, resolve, reject, data, method)
    })
  }

  var goRequest = function(url, resolve, reject, data = {}, method = "GET") {
    
    wx.request({
      url: config.api_base_url + url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: function (res) {
        var code = res.data.code;
        // var startChar = code.charAt(0);
        if (code == '1') {
          resolve(res.data)
          console.log(res.data)
        } else {
          reject()
          wx.showToast({
            title: '网络错误',
            duration: 2000
          })
        }
      },
      fail: function (err) {
        reject()
        wx.showToast({
          title: '网络错误',
          duration:2000
        })
      }
    });
  }
// }

// export { HTTP };
module.exports = {
  request
}